package com.retirement.records.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.retirement.common.utils.bean.BeanUtils;
import com.retirement.information.domain.OldInformation;
import com.retirement.information.mapper.OldInformationMapper;
import com.retirement.records.dto.OldHealthRecordsPageDto;
import com.retirement.records.vo.OldHealthRecordsPVo;
import com.retirement.records.vo.OldHealthRecordsPageVo;
import com.retirement.records.domain.OldHealthRecords;
import com.retirement.records.mapper.OldHealthRecordsMapper;
import com.retirement.records.service.IOldHealthRecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijy
 * @since 2023-12-21
 */
@Service
public class OldHealthRecordsServiceImpl extends ServiceImpl<OldHealthRecordsMapper, OldHealthRecords> implements IOldHealthRecordsService {

    private  final OldInformationMapper oldInformationMapper;

    public OldHealthRecordsServiceImpl(OldInformationMapper oldInformationMapper) {
        this.oldInformationMapper = oldInformationMapper;
    }

    @Override
    public List<OldHealthRecordsPageVo> getList(OldHealthRecordsPageDto dto) {
        return baseMapper.getList(dto);
    }

    @Override
    public OldHealthRecordsPVo getDetail(String id) {
        OldInformation oldInformation = oldInformationMapper.selectOne(new LambdaQueryWrapper<OldInformation>()
                .eq(OldInformation::getId,id).eq(OldInformation::getDeleted,0));
        Assert.isTrue(ObjectUtil.isNotNull(oldInformation), "无此档案关联的用户信息");
        OldHealthRecords oldHealthRecords = baseMapper.selectOne(new LambdaQueryWrapper<OldHealthRecords>()
                .eq(OldHealthRecords::getInformationId,id).eq(OldHealthRecords::getDeleted,0));
        Assert.isTrue(ObjectUtil.isNotNull(oldHealthRecords), "无档案信息");
        OldHealthRecordsPVo vo = new OldHealthRecordsPVo();
        BeanUtils.copyProperties(oldHealthRecords,vo);
        vo.setName(oldInformation.getName());
        vo.setGender(oldInformation.getGender());
        vo.setAge(oldInformation.getAge());

        return vo;
    }

    @Override
    public int addRecords(OldHealthRecords dto) {
        Assert.isTrue(ObjectUtil.isNotNull(dto.getInformationId()), "无用户id信息");
        OldInformation bean = oldInformationMapper.selectOne(new LambdaQueryWrapper<OldInformation>()
                .eq(OldInformation::getId,dto.getInformationId())
                .eq(OldInformation::getDeleted,0));
        Assert.isTrue(ObjectUtil.isNotNull(bean), "无用户信息");
        dto.setId(IdUtil.simpleUUID());
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());

        return baseMapper.insert(dto);
    }

    @Override
    public int updateRecords(OldHealthRecords dto) {
        OldHealthRecords bean = baseMapper.selectById(dto.getId());
        Assert.isTrue(ObjectUtil.isNotNull(bean), "无档案信息");
        dto.setUpdateTime(LocalDateTime.now());

        return baseMapper.updateById(dto);
    }

    @Override
    public int deleteRecords(String id) {
        OldHealthRecords bean = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(bean), "无档案信息");
        bean.setDeleted(1);

        return baseMapper.updateById(bean);
    }


}
